<?php

namespace Wx\Controller;

use Think\Controller;



class MyAddressController extends CommonController {

    // 初始化方法
    public function _initialize(){
        $this->u_id = session('u_id');
        $this->addr = M('address');
    }//end fun _initialize()

    public function test(){
        $this -> display();
    }

//    普通用户-地址管理
    public function index(){
        $list = $this->addr->table(C('DB_PREFIX')."address as a")
                ->join(C('DB_PREFIX')."shop_community as c ON c.id=a.community_id")
                ->field('a.*,c.title,c.a_id_1,c.a_id_2,c.a_id_3')
				->where("u_id=".session("u_id"))
                ->order('is_default desc')
                ->select();
        foreach($list as $k => $v){
            $list[$k]['a_1_name'] = M('area')->where('id='.$v['a_id_1'])->getField('name');
            $list[$k]['a_2_name'] = M('area')->where('id='.$v['a_id_2'])->getField('name');
            $list[$k]['a_3_name'] = M('area')->where('id='.$v['a_id_3'])->getField('name');
        }
        $this -> assign('list',$list);
        $this -> display('addressManage');
    }// end func index()


    // 添加修改地址信息
    public function addrInfo(){
        $id = intval(I('id'));
        if (IS_POST) {
            $data = I('post.');
            $a_id=explode('-',$data['a_id']);//dump($a_id);die;
            $data['a_id_1']=(int)$a_id[0];
            $data['a_id_2']=(int)$a_id[1];
            $data['a_id_3']=(int)$a_id[2];
            // 判断小区表中是否已经存在该小区
            $info = M('shop_community')->where(array('title'=>$data['title'],'a_id_1'=>$data['a_id_1'],'a_id_2'=>$data['a_id_2'],'a_id_3'=>$data['a_id_3']))->find();
            if ($data['id']) {//编辑
                $data['community_id'] = $info['id'];
                $res = $this->edit($data);
            } else {//添加
                $data['is_community_id'] = $info['id'];
                $res = $this->add($data);
            }// end if

            if ($res) {
                $this->success('提交成功', U('Wx/MyAddress/index'));
            } else {
                $this->error('提交失败');
            }// end if

        }else{
            if ($id) {
                $data['addrInfo'] = $this->addr->where(array('id'=>$id,'u_id'=>$this->u_id))->find();
                // 省市区信息+小区信息
                $c_info = M('shop_community')->where(array('id' => $data['addrInfo']['community_id'] ))->find();
                $c_info['a_1_name'] = M('area')->where('id='.$c_info['a_id_1'])->getField('name');
                $c_info['a_2_name'] = M('area')->where('id='.$c_info['a_id_2'])->getField('name');
                $c_info['a_3_name'] = M('area')->where('id='.$c_info['a_id_3'])->getField('name');
                $data['c_info'] = $c_info;
                $data['title'] = '编辑地址';
            } else {
                $data['addrInfo'] = 1;
                $data['c_info'] = 1;
                $data['title'] = '新增地址';
            }
            // 查询一级-省的地址信息
            //地址js生成
            $ali=set_area_js();
            $this->assign('ali',$ali);

            $data['province'] = M('area')->where('lev = 1')->select();
            $this->assign($data);
            $this->display('address');

        }// end if

    }//end fun addrInfo()


    // 添加修改地址信息
    public function add($data){
        $count = $this->addr->where('u_id = '. $this->u_id)->count();
        if($count==5){
            $this->error('您的收货地址已经有五个了');
        }
        if($data['community_id']==''){
            $data['community_id'] = $this->addArea($data);
        }
        $data['u_id'] = $this->u_id;
        $data['add_time'] = time();
        $res = $this->addr->add($data);
        return $res;

    }//end fun addAddr()


    // 添加修改地址信息
    public function edit($data){
        $id = intval($data['id']);
        $map['id'] = $id;
        $map['u_id'] = $this->u_id;
        // 对比原小区的数据与提交的小区的数据，有差距则重新添加一条小区信息
        $addrInfo = $this->addr->where($map)->find();
        $c_info = M('shop_community')->where(array('id' => $addrInfo['community_id'] ))->find();
        // 如果地址有变动，则更新新地址信息
        if($data['title']!=$c_info['title']||$c_info['a_id_1']!=$data['a_id_1']||$c_info['a_id_2']!=$data['a_id_2']||$c_info['a_id_3']!=$data['a_id_3']){
            unset($data['id']);
            // 清除原有的小区id
            if($data['is_community_id']){
                $data['community_id'] = $data['is_community_id'];
            }else{
                unset($data['community_id']);
                $data['community_id'] = $this->addArea($data);
            }
        }
        $res = $this->addr->where($map)->save($data);
        if($res==0){// 暂时不修改也要提交成功
            $res = 1;
        }
        return $res;

    }//end fun editAddr()


    //获取下一级所有地址
    public function getnext(){
        if(IS_AJAX){
            $area = D('area');
            $getall = $area->where("`re_id` =".$_GET['rid'])->select();
            $this->ajaxReturn($getall);
        }else{
            $this->error('非法进入');
        }// end if

    }//end fun getnext()


    // 删除地址
    public function delete(){

        if(IS_POST){

            $addr_id = intval(I('addr_id'));

            $res = $this->addr->where(array('id'=>$addr_id,'u_id'=>$this->u_id))->delete();

            if($res){

                $this->success('删除成功');

            }else{

                $this->error('删除成功，请检查');

            }

        }else{

            $this->error('非法进入');

        }// end if



    }// end fun delete()





    // 修改默认地址

    public function changeDefault(){

        if(IS_POST){

            // 获取设置为默认地址的id

            $addr_id = intval(I('addr_id'));

            // 查询原默认地址信息

            $default = getInfo('address',array('is_default'=>1,'u_id'=>$this->u_id),'u_id,is_default,id');

            // 修改默认 和 原默认

            $res = $this->addr->where('id = '.$addr_id)->setField('is_default',1);

            if($default){

                $res1 = $this->addr->where('id = '.$default['id'])->setField('is_default',0);

            }

            if($res){

                $this->success('修改成功');

            }else{

                $this->error('修改失败，请检查');

            }

        }else{

            $this->error('非法进入');

        }// end if

    }





    // 添加小区信息 返回值$res：新增数据的id

    public function addArea($data){

        // 清除前后空格

        $data['title'] =  str_replace(' ', '', $data['title']);

        $res = M('shop_community')->add($data);

        return $res;

    }// end fun addArea()



}